<div class="btn-group">
    <input id='select-all' type="checkbox" class="grid-select-all" />&nbsp;<label for="select-all">全选</label>
</div>
<div class="btn-group" style="margin-left: 15px;">
    <button class="btn btn-xs btn-twitter plan-confirm"><i class="fa fa-check"></i> 确认</button>
</div>
<script language="JavaScript" type="text/javascript">
    $(function () {
        $('.grid-select-all').iCheck({checkboxClass:'icheckbox_minimal-blue'});
        $('.grid-select-all').on('ifChanged', function(event) {
            if (this.checked) {
                $('.grid-row-checkbox').iCheck('check');
            } else {
                $('.grid-row-checkbox').iCheck('uncheck');
            }
        });

        var selectedRows = function () {
            var selected = [];
            $('.grid-row-checkbox:checked').each(function(){
                selected.push($(this).data('id'));
            });
            return selected;
        };

        var checkForm = function (pSkuIds = []) {
            if(pSkuIds.length == 0){
                swal('请选择要操作的记录！', '', 'error');
                return false;
            }
            for(var index in pSkuIds) {
                var pSkuId = pSkuIds[index];
                var psku_code = $('#psku_code_' + pSkuId).val();
                var supplier_id = $('#supplier_id_' + pSkuId).val();
                if(!supplier_id){
                    swal('请为' + psku_code + '选择供应商', '', 'error');
                    return false;
                }
                var this_plan_num = $('#this_plan_num_' + pSkuId).val();
                if(!this_plan_num.trim() || isNaN(this_plan_num) || this_plan_num == 0){
                    swal('请为' + psku_code + '录入计划采购数量', '', 'error');
                    return false;
                }
            }
            return true;
        };

        var getParams = function (pSkuIds = []) {
            var selected = [];
            for(var index in pSkuIds) {
                var pSkuId = pSkuIds[index];
                var _selected = {};
                _selected.psku_id = pSkuId;
                _selected.supplier_id = $('#supplier_id_' + pSkuId).val();
                _selected.apply_detail_total = $('#apply_detail_total_' + pSkuId).val();
                _selected.planned_num_total = $('#planned_num_total_' + pSkuId).val();
                _selected.this_plan_num = $('#this_plan_num_' + pSkuId).val();
                var applyDetail = [];
                $('#sku_' + pSkuId + ' input.apply-detail').each(function () {
                    applyDetail.push($(this).val());
                });
                _selected.apply_detail = applyDetail;
                selected.push(_selected);
            }
            return selected;
        };

        $('.plan-confirm').on('click', function(e) {
            e.preventDefault();
            var idArr = selectedRows();
            // console.log(idArr);return false;
            if(checkForm(idArr)){
                var params = getParams(idArr);
                // console.log(params);return false;
                swal({
                    title: "确定生成采购单吗？",
                    text : "系统将按【供应商】分组生成多张采购单",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    showLoaderOnConfirm: true,
                    cancelButtonText: "取消",
                    preConfirm: function() {
                        return new Promise(function(resolve) {
                            $.ajax({
                                method: 'post',
                                url: '/xqc/purchase/exe-plan',
                                data: {
                                    _token:"{{ csrf_token() }}",
                                    param: params,
                                },
                                success: function (data) {
                                    $.pjax.reload("#plan-confirm-form");
                                    resolve(data);
                                }
                            });
                        });
                    }
                }).then(function(result) {
                    var data = result.value;
                    if (typeof data === 'object') {
                        if (data.status) {
                            toastr.success('提交成功 !');
                        } else {
                            swal(data.message, '', 'error');
                        }
                    }
                });
            }
        });
    });
</script>
